<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">刷新</el-button>
        <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hotel:room:add']"
        >新增</el-button>
        </el-col>
      </el-form-item>
    </el-form>
    

    <el-table v-loading="loading" :data="roomList">
      <el-table-column label="房间号" align="center" prop="roomId" v-if="true"/>
      <el-table-column label="房间类型" align="center" prop="roomType" />
      <el-table-column label="房间状态" align="center" prop="roomStatus" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button @click="handleSet(scope.row)" >更改</el-button>

        </template>

      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间号">
          <el-input v-model="form.roomId" />
        </el-form-item>

        <el-form-item label="房间类型">

          <el-select v-model="form.roomType">

            <el-option value="single" />
            <el-option value="double" />
            <el-option value="executive" />
            <el-option value="deluxe" />

          </el-select>

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <el-dialog :title="title" :visible.sync="setOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>

          <el-select v-model="form.roomStatus">

            <el-option value="f" label="空闲"/>
            <el-option value="w" label="工作中"/>

          </el-select>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="setRoom">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoom, getRoom, delRoom, addRoom, updateRoom,setRoomStatus } from "@/api/hotel/room";

export default {
  name: "Room",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      setOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomType: undefined,
        roomStatus: undefined
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        roomId: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        roomType: [
          { required: true, message: "不能为空", trigger: "change" }
        ],
        roomStatus: [
          { required: true, message: "不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.setOpen=false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roomId: undefined,
        roomType: undefined,
        roomStatus: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roomId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const roomId = row.roomId || this.ids
      getRoom(roomId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改房间状态";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          
            addRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          
        }
      });
    },

    handleSet(row) {
      if(row.roomStatus!="o"){
        this.reset();
        this.setOpen = true;
        this.title = "修改房间状态";
        this.form=row;
      }
      else{
        this.$modal.confirm("该房间有顾客使用中！");
      }
    },

    
    //修改房间状态
    setRoom(){
        setRoomStatus(this.form).then(response => {
            this.$modal.msgSuccess("更改成功");
            this.getList();
          }).finally(() => {
              this.cancel();
        });
        
    },

    statusFormat(row,column){
      if(row.roomStatus=="f"){
        return "空闲";
      }
      else if(row.roomStatus=="w"){
        return "工作中";
      }
      else if(row.roomStatus=="o"){
        return "使用中";
      }
    }

    
  }
};
</script>
